<template>
  <div class="register-container">
    <h2>物流系统注册</h2>
    <form @submit.prevent="handleRegister">
      <div class="form-group">
        <label>登录账号：</label>
        <input 
          type="text" 
          v-model="account" 
          placeholder="请输入登录账号（唯一）"
          required
        >
      </div>
      <div class="form-group">
        <label>登录密码：</label>
        <input 
          type="password" 
          v-model="password" 
          placeholder="请输入登录密码"
          required
        >
      </div>
      <div class="form-group">
        <label>注册邮箱：</label>
        <input 
          type="email" 
          v-model="email" 
          placeholder="请输入注册邮箱"
          required
        >
      </div>
      <div class="form-group">
        <label>用户姓名：</label>
        <input 
          type="text" 
          v-model="name" 
          placeholder="请输入用户姓名"
          required
        >
      </div>
      <div class="error-msg" v-if="errorMessage">{{ errorMessage }}</div>
      <button type="submit" class="submit-btn">立即注册</button>
      <div class="login-link">
        已有账号？<router-link to="/login">立即登录</router-link>
      </div>
    </form>
  </div>
</template>

<script>
import { userRegister } from '@/api/user'

export default {
  name: 'UserRegister',
  data() {
    return {
      account: '',
      password: '',
      email: '',
      name: '',
      errorMessage: ''
    }
  },
  methods: {
    async handleRegister() {
      // 基础表单验证
      if (!this.account || !this.password || !this.email || !this.name) {
        // 使用Element Plus的错误消息提示
        this.$message.error('请填写所有注册信息')
        return
      }

      try {
        const res = await userRegister({
          account: this.account,
          password: this.password,
          email: this.email,
          name: this.name
        })
        console.log(res);
        
        // 使用Element Plus的成功消息提示
        this.$message.success('注册成功，请登录')
        this.$router.push('/login') // 跳转登录页
      } catch (err) {
        // 使用Element Plus的错误消息提示
        this.$message.error(err.message || '注册失败，请重试')
      }
    }
  }
}
</script>

<style scoped>
.register-container {
  max-width: 400px;
  margin: 50px auto;
  padding: 30px;
  box-shadow: 0 0 10px rgba(0,0,0,0.1);
}
.form-group {
  margin-bottom: 20px;
}
.form-group label {
  display: block;
  margin-bottom: 8px;
  font-size: 14px;
}
.form-group input {
  width: 100%;
  padding: 8px;
  border: 1px solid #ddd;
  border-radius: 4px;
}
.submit-btn {
  width: 100%;
  padding: 10px;
  background: #42b983;
  color: white;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  font-size: 16px;
}
.submit-btn:hover {
  background: #359d6d;
}
.error-msg {
  color: #ff4d4f;
  margin: 10px 0;
  font-size: 14px;
}
.login-link {
  margin-top: 15px;
  text-align: center;
  font-size: 14px;
}
.login-link a {
  color: #42b983;
  text-decoration: none;
}
</style>